<?php
defined('IN_IXDCW') or exit('Access Denied');
$BRAND = array();
$do    = new LoanModule($moduleid);
$arcid = isset($arcid) ? $arcid : 0;
$arbid = isset($arbid) ? $arbid : 0;
$armid = isset($armid) ? $armid : 0;
$alid  = isset($alid) ? $alid : 0;
$menus = array(
	array('添加' . $MOD['name'], '?moduleid=' . $moduleid . '&action=add'),
	array($MOD['name'] . '列表', '?moduleid=' . $moduleid),
);

if ($_catids) require DT_ROOT . '/admin/admin_check.inc.php';

if (in_array($action, array('', 'check', 'reject', 'recycle'))) {
	$sfields = array('模糊');
	$dfields = array('title');
	$sorder  = array('结果排序方式', '添加时间降序', '添加时间升序', '更新时间降序', '更新时间升序', '信息ID降序', '信息ID升序');
    //$dorder  = array('listorder ASC', 'addtime DESC', 'addtime ASC', 'edittime DESC', 'edittime ASC', 'id DESC', 'id ASC');
	$dorder  = array( 'addtime DESC', 'addtime ASC', 'edittime DESC', 'edittime ASC', 'id DESC', 'id ASC');
	isset($fields) && isset($dfields[$fields]) or $fields = 0;
	isset($order) && isset($dorder[$order]) or $order = 0;
	$level = isset($level) ? intval($level) : 0;

	isset($datetype) && in_array($datetype, array('edittime', 'addtime')) or $datetype = 'addtime';

	$fields_select = dselect($sfields, 'fields', '', $fields);
	$level_select  = level_select('level', '级别', $level);
	$order_select  = dselect($sorder, 'order', '', $order);

	$condition = '';
	if ($catid) $condition .= " And catid={$catid}";

	if ($keyword) $condition .= " AND $dfields[$fields] LIKE '%$keyword%'";
	$timetype = strpos($dorder[$order], 'edit') === false ? 'add' : '';


}



switch ($action) {
	case 'add':

		if ($submit) {
			if ($do->pass($post)) {
                $post['status'] = 3;
				$do->add($post);
			//	Cookie::set('picture_upload_arids', "{$post['arbid']},{$post['arcid']},{$post['amid']},{$post['alid']},{$post['title']}");
				Prompt::message('添加成功！', Env::referer());
			} else {
				Prompt::message($do->getErrmsg());
			}
		}

		if (Cookie::has('picture_upload_arids')) {
			$arids = explode(',', Cookie::get('picture_upload_arids'));
			$arbid = $arids[0];
			$arcid = $arids[1];
			$amid  = $arids[2];
			$alid  = $arids[3];
			$title = $arids[4];
		}
		include tpl('edit', $module);
		break;

	case 'edit':
		$itemid or msg();
		$do->setItemid($itemid);
        if ($submit) {
            if ($do->pass($post)) {

                $do->edit($post);

                dmsg('修改成功', '?moduleid=' . $moduleid);
            } else {


                msg($do->getErrmsg());
            }
        } else {


            $item = $do->getOne();

            extract($item);

       //  $pagebreak = strpos($item['content'], '[pagebreak]') === false ? 0 : 1;


            $addtime   = timetodate($addtime);
            $menuon    = array('4', '3', '2', '1');

            $menuid    = $menuon[$status];

            $tname     = '修改' . $MOD['name'];

            include tpl($action, $module);

        }

		break;


	case 'update':

		is_array($itemid) or msg('请选择' . $MOD['name']);
		foreach ($itemid as $v) {
			$do->update($v);
		}
		dmsg('更新成功', $forward);
		break;
	case 'delete':

        $itemid or msg('请选择' . $MOD['name']);
        $do->delete($itemid);
        dmsg('删除成功', $forward);
        break;


	case 'restore':
		$apid or msg('请选择' . $MOD['name']);
		$do->restore($apid);
		dmsg('还原成功', $forward);
		break;
	case 'clear':
		$do->clear();
		dmsg('清空成功', $forward);
		break;
	case 'level':
		$apid or msg('请选择' . $MOD['name']);
		$level = intval($level);
		$do->level($apid, $level);
		dmsg('级别设置成功', $forward);
		break;
	case 'recycle':
		$lists  = $do->getList('status=0' . $condition, $dorder[$order]);
		$menuid = 4;
		include tpl('index', $module);
		break;
	case 'reject':
		if ($apid && !$psize) {
			$do->reject($apid);
			dmsg('拒绝成功', $forward);
		} else {
			$lists  = $do->getList('status=1' . $condition, $dorder[$order]);
			$menuid = 3;
			include tpl('index', $module);
		}
		break;
	case 'check':
		if ($apid && !$psize) {
			$do->check($apid);
			dmsg('审核成功', $forward);
		} else {
			$lists  = $do->getList('status=2' . $condition, $dorder[$order]);
			$menuid = 2;
			include tpl('index', $module);
		}
		break;
	default:

        $lists = $do->getList('status=3'.$condition, $dorder[$order]);

        $menuid = 1;
        include tpl('index', $module);
        break;

}